<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item
          :hidden="true"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['id']"
          />
        </a-form-item>

        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="工作中心编码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="工作中心编码"
                :disabled="editable || showable"
                v-decorator="['code', {rules: [{required: true, message: '请输入工作中心编码'}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="工作中心名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="工作中心名称"
                :disabled="showable"
                v-decorator="['name', {rules: [{required: true, message: '请输入工作中心名称'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="产线"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <lines-select-madal :disabled="showable" v-decorator="['lineCode',{rules: [{required: true, message: '请选择产线'}]}]" title='选择产线'></lines-select-madal>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="设备"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <device-select-madal :disabled="showable" v-decorator="['deviceCode', {rules: [{required: true, message: '请选择设备'}]}]" title='选择设备'></device-select-madal>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="工位"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <station-select-madal :disabled="showable" v-decorator="['stationCode', {rules: [{required: true, message: '请选择工位'}]}]"></station-select-madal>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="工作模式"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >

              <a-select :disabled="showable" v-decorator="['mode', {rules: [{required: true, message: '请选择工作模式'}],initialValue: 'input'}]" :options='modeSelect' @change="handleChangeMode"></a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="输入物料"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="输入物料"
                :disabled="showable"
                v-decorator="['inputMats', {rules: [{required: true, message: '请选择输入物料'}]}]"/>
            </a-form-item>
          </a-col>

        </a-row>

        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="排序"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="排序"
                :disabled="showable"
                v-decorator="['orderNum', {rules: [{required: true, message: '请选择输入物料'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <div v-show='mode === "output"'>
          <a-divider orientation="left">报工参数选择</a-divider>
          <a-row :getter="24">
            <a-col :md="12" :sm="24">
              <a-form-item
                label="报工模式"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
              >
                <a-select :disabled="showable" v-decorator="['type', {rules: [{required: true, message: '请选择报工模式'}]}]" :options='typeSelect'></a-select>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="倒冲模式"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
              >
                <a-select :disabled="showable" v-decorator="['backflushMode', {rules: [{required: true, message: '请选择倒冲模式'}]}]" :options='backflushModeSelect'></a-select>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row :getter="24">
            <a-col :md="12" :sm="24">
              <a-form-item
                label="报工类型"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
              >

                <a-select :disabled="showable" v-decorator="['outputType', {rules: [{required: true, message: '请选择报工类型'}]}]" :options='outputTypeSelect'></a-select>
              </a-form-item>
            </a-col>

          </a-row>
        </div>


      </a-form>
    </a-spin>
  </a-modal>

</template>
<script>
import pick from 'lodash.pick'
import { FormMixin } from '@/mixins/FormMixin'
import { getObj, saveObj } from '@/api/base/workCenter'
import LinesSelectMadal from '@/views/main/base/lines/linesSelectMadal'
import DeviceSelectMadal from '@/views/main/base/device/deviceSelectMadal'
import stationSelectMadal from '@/views/main/base/station/stationSelectMadal'

export default {
  name: 'WorkCenterForm',
  components: { DeviceSelectMadal, LinesSelectMadal, stationSelectMadal },
  mixins: [FormMixin],
  data () {
    return {
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      mode: 'input',
      typeSelect: [
        {
          title: '半品',
          value: 'semi'
        },
        {
          title: '成品',
          value: 'finish'
        }
      ],
      modeSelect: [
        {
          title: '投料',
          value: 'input'
        },
        {
          title: '报工',
          value: 'output'
        }
      ],
      outputTypeSelect: [
        {
          title: '单码报工',
          value: 'singleCode'
        },
        {
          title: '合并报工(根据装箱规则自动生成对应的箱码)',
          value: 'mergeCode'
        },
        {
          title: '数量报工',
          value: 'num'
        }
      ],
      backflushModeSelect: [
        {
          title: '按bom倒冲',
          value: 'bom'
        },
        {
          title: '按输入物料倒冲',
          value: 'input'
        }
      ]
    }
  },

  methods: {

    handleChangeMode(e){
      console.log(e,'eeee')
      this.mode = e
    },

    // 获取信息
    edit (id, type) {
      this.mode = 'input'
      this.visible = true
      const { form: { setFieldsValue, resetFields } } = this
      resetFields()
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          this.mode = res.data.mode
          const record = res.data
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'lineCode','code', 'name','deviceCode','stationCode','inputMats','orderNum','type','outputType','mode','backflushMode']))
          })
        }).catch((err) => {
          this.$message.error(err.msg)
        }).finally(_ => {
          this.confirmLoading = false
        })
      }
    },

    // 结束保存
    handleOk () {
      let field = []
      if(this.mode === 'input'){
        field = ['id','lineCode','code','name','deviceCode','stationCode','inputMats','orderNum','mode']
      }else{
        field = ['id','lineCode','code','name','deviceCode','stationCode','inputMats','orderNum','mode','type','outputType','backflushMode']
      }
      this.form.validateFields(field,async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          try {
            await saveObj(values)
            this.confirmLoading = false
            this.handleCancel()
            this.$emit('ok')
          } catch (e) {
            this.$message.error(e.msg)
            this.confirmLoading = false
          }
        }
      })
    }

  }
}
</script>

<style scoped>

</style>